package com.ai.boot.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author Li9527.
 * @Description
 * @create 2025/6/22 上午10:58
 */

@RestController
@RequestMapping("/api/ds")
public class ChatAiController {
    @Autowired
    private ChatModel chatModel;

    @Autowired
    private ChatClient chatClient;

    //非流式响应
    @GetMapping("/simple0")
    public String xyy(@RequestParam(value = "message") String message) {
//        return chatClient.prompt().user(message).call().content();
        ChatResponse chatResponse = chatModel.call(
                new Prompt(
                        message,
                        ChatOptions.builder()
                                .model("deepseek-reasoner")
                                .temperature(0.8)
                                .build()
                )
        );
        String content = chatResponse.getResult().getOutput().getContent();
        return content;
    }

    //流式响应      produces:设置编码方式
    @GetMapping(value = "/stream", produces = "text/html;charset=utf8")
    public Flux<String> xyyStream(@RequestParam(value = "message") String message) {
        return chatClient.prompt().user(message).stream().content();
    }

}
